//
//  LCInviteAdViewController.m
//  lechuang
//
//  Created by Mr_hi on 2025/4/18.
//  Copyright © 2025 Mooyi. All rights reserved.
//

#import "LCInviteAdViewController.h"
#import "LCInviteAdPosterView.h"
#import "OTPageScrollView.h"
#import "OTPageView.h"

#import "TZImageManager.h"
#import "LCInviteShareView.h"

@interface LCInviteAdViewController ()<OTPageScrollViewDataSource,OTPageScrollViewDelegate>

@property (strong, nonatomic)NSArray * dataArr;

@property (weak, nonatomic) IBOutlet UIView *contentView;

@property (strong, nonatomic)OTPageView * psView;

@end

@implementation LCInviteAdViewController

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
}

-(void)configUI {
    CGFloat height = ScreenH - NavgationHeight - TabRelativeBottomH - 78;
    
    
    OTPageView * psView = [[OTPageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, height)];
    psView.pageScrollView.dataSource = self;
    psView.pageScrollView.delegate = self;
    psView.pageScrollView.padding = 30;
    psView.pageScrollView.leftRightOffset = 0;
    psView.pageScrollView.frame = CGRectMake(35, 0, ScreenW - 100 + 30, height);
    psView.backgroundColor = [UIColor clearColor];
    
    self.dataArr = @[@"lc_invite_poster_bg0",@"lc_invite_poster_bg1",@"lc_invite_poster_bg2",@"lc_invite_poster_bg3"];
    
    [psView.pageScrollView reloadData];
    [self.contentView addSubview:psView];
    
    self.psView = psView;
    
}

- (NSInteger)numberOfPageInPageScrollView:(OTPageScrollView*)pageScrollView{
    return self.dataArr.count;
}

- (UIView*)pageScrollView:(OTPageScrollView*)pageScrollView viewForRowAtIndex:(int)index {
    LCInviteAdPosterView * posterView = [[LCInviteAdPosterView alloc] initForNib];
    posterView.bgView.image = [UIImage imageNamed:self.dataArr[index]];
    return posterView;
}

- (CGSize)sizeCellForPageScrollView:(OTPageScrollView*)pageScrollView {
    CGFloat cellW = ScreenW - 100;
    CGFloat cellH = ceil(cellW * 533/300.0);
    return CGSizeMake(cellW, cellH);
}

- (IBAction)saveImgBtnAction:(UIButton *)sender {
    NSInteger index = self.psView.pageScrollView.contentOffset.x / self.psView.pageScrollView.frame.size.width;
    UIView * cell = [self.psView.pageScrollView viewForRowAtIndex:index];
    
    UIImage * img = [UIImage imageCaptureWithView:cell];
    [[TZImageManager manager] savePhotoWithImage:img completion:^(PHAsset *asset, NSError *error) {
        if (!error && asset) {
            [self showSuccessText:@"图片已保存"];
        }
    }];
}

- (IBAction)inviteBtnAction:(UIButton *)sender {
    NSInteger index = self.psView.pageScrollView.contentOffset.x / self.psView.pageScrollView.frame.size.width;
    UIView * cell = [self.psView.pageScrollView viewForRowAtIndex:index];
    
    UIImage * img = [UIImage imageCaptureWithView:cell];
    
    LCInviteShareView * view = [[LCInviteShareView alloc] initForNibWithShareImg:img];
    view.parentVC = self;
    [view show];
}


@end
